/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */
package apollo.generator.modules.controller.sys;


import apollo.generator.common.annotation.SysLog;
import apollo.generator.common.base.BaseController;
import apollo.generator.common.base.R;
import apollo.generator.common.base.RR;
import apollo.generator.common.exception.CustomException;
import apollo.generator.modules.entity.SysConfigEntity;
import apollo.generator.modules.service.SysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统配置信息
 */
@RestController
@RequestMapping("/sys/config")
public class SysConfigController extends BaseController {
	@Autowired
	private SysConfigService sysConfigService;
	
	/**
	 * 所有配置列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:config:list")
	public R list(@RequestParam Map<String, Object> params) throws NoSuchFieldException, IllegalAccessException, CustomException {
		return RR.success(sysConfigService.queryPage(params));
	}
	
	
	/**
	 * 配置信息
	 */
	@GetMapping("/info/{id}")
	@RequiresPermissions("sys:config:info")
	public R info(@PathVariable("id") Long id) throws NoSuchFieldException, IllegalAccessException {
		return RR.success(sysConfigService.getById(id));
	}
	
	/**
	 * 保存配置
	 */
	@SysLog("保存配置")
	@PostMapping("/save")
	@RequiresPermissions("sys:config:save")
	public R save(@RequestBody SysConfigEntity config) throws CustomException {
		validator2CustomException(config);
		sysConfigService.saveConfig(config);
		return RR.success();
	}
	
	/**
	 * 修改配置
	 */
	@SysLog("修改配置")
	@PostMapping("/update")
	@RequiresPermissions("sys:config:update")
	public R update(@RequestBody SysConfigEntity config) throws CustomException {
		validator2CustomException(config);
		sysConfigService.update(config);
		return RR.success();
	}
	
	/**
	 * 删除配置
	 */
	@SysLog("删除配置")
	@PostMapping("/delete")
	@RequiresPermissions("sys:config:delete")
	public R delete(@RequestBody Long[] ids){
		sysConfigService.deleteBatch(ids);
		return RR.success();
	}

}
